-
Notifications
You must be signed in to change notification settings - Fork 373
[Feature] add support for a custom CSVFormat #353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
# Conflicts: # fastexcel-core/src/main/java/cn/idev/excel/read/metadata/ReadWorkbook.java
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds support for a custom CSVFormat when reading CSV files by introducing a new csvFormat parameter in the read API and updating the CSV reading behavior accordingly.
- Added custom CSV test cases with different delimiters and quoting configurations.
- Extended ReadWorkbook to store a CSVFormat and updated CsvReadWorkbookHolder to use it.
- Introduced a new csvFormat() method in ExcelReaderBuilder to allow users to specify a custom CSVFormat.
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
File | Description |
---|---|
fastexcel-test/src/test/resources/csv/simple.csv | Added simple CSV test file with custom header and numeric data. |
fastexcel-test/src/test/resources/csv/simple-quote.csv | Added CSV test file for verifying custom quote handling. |
fastexcel-test/src/test/resources/csv/simple-delimiter.csv | Added CSV test file to test custom delimiter functionality. |
fastexcel-test/src/test/java/cn/idev/excel/test/temp/csv/CsvFormatTest.java | Created tests for CSVFormat support using both default and custom formats. |
fastexcel-core/src/main/java/cn/idev/excel/read/metadata/holder/csv/CsvReadWorkbookHolder.java | Modified to initialize csvFormat based on a provided value or fallback to CSVFormat.DEFAULT. |
fastexcel-core/src/main/java/cn/idev/excel/read/metadata/ReadWorkbook.java | Added a new field for CSVFormat with associated documentation. |
fastexcel-core/src/main/java/cn/idev/excel/read/builder/ExcelReaderBuilder.java | Added a new csvFormat() method to allow chaining of CSVFormat configuration. |
fastexcel-core/src/main/java/cn/idev/excel/read/metadata/holder/csv/CsvReadWorkbookHolder.java
Outdated
Show resolved
Hide resolved
hi,@psxjoy Please don't deal with this PR for now. I think it should be supported to write to the file, I'm working on this task. |
Two suggestions here,
|
Hi,@psxjoy |
Set CSVformatAs #352 mentioned issue easyexcel-issue-3868. WriteCSVFormat csvFormat = CSVFormat.DEFAULT.builder().setDelimiter(CsvConstant.AT).build();
csvFile = TestFileUtil.createNewFile("csv-delimiter.csv");
try (ExcelWriter excelWriter = FastExcel.write(csvFile, CsvData.class).excelType(ExcelTypeEnum.CSV).build()) {
WriteWorkbookHolder writeWorkbookHolder = excelWriter.writeContext().writeWorkbookHolder();
Workbook workbook = writeWorkbookHolder.getWorkbook();
if (workbook instanceof CsvWorkbook) {
CsvWorkbook csvWorkbook = (CsvWorkbook) workbook;
csvWorkbook.setCsvFormat(csvFormat);
writeWorkbookHolder.setWorkbook(csvWorkbook);
}
WriteSheet writeSheet = FastExcel.writerSheet(0).build();
excelWriter.write(csvDataList, writeSheet);
} ReadCSVFormat csvFormat = CSVFormat.DEFAULT.builder().setDelimiter(CsvConstant.AT).build();
csvFile = TestFileUtil.readFile("csv-delimiter.csv");
try (ExcelReader excelReader = FastExcel.read(csvFile, CsvData.class, new CsvDataListener()).build()) {
ReadWorkbookHolder readWorkbookHolder = excelReader.analysisContext().readWorkbookHolder();
if (readWorkbookHolder instanceof CsvReadWorkbookHolder) {
CsvReadWorkbookHolder csvReadWorkbookHolder = (CsvReadWorkbookHolder) readWorkbookHolder;
csvReadWorkbookHolder.setCsvFormat(csvFormat);
}
ReadSheet readSheet = FastExcel.readSheet(0).build();
excelReader.read(readSheet);
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very good PR, I think it complements the CSV format very well.
Close #352
What's changed?
Features
Example
Read
Write
Checklist